<template>
  <div class="com-container">
    <div class="com-echart" ref="rank_ref"></div>
  </div>
</template>

<script>
import { mapState } from 'vuex';
export default {
  data() {
    return {
      echartInstance: null,
      // 服务器返回的数据
      allData: null,
      // 区域缩放起点值
      startValue: 0,
      // 区域缩放终点值
      endValue: 9,
      timerId: null
    };
  },
  computed:{
    ...mapState(['theme']),
  },
  watch:{
    theme(){
      // 销毁当前图表
      this.echartInstance.dispose();
      this.initChart();
      this.screenAdapter();
      this.updateChart();
    }
  },
  mounted() {
    this.initChart();
    this.getData();
    window.addEventListener("resize", this.screenAdapter);
    // 首次适配
    this.screenAdapter();
  },
  beforeDestroy() {
    if (this.timerId) {
      clearInterval(this.timerId);
      this.timerId = null;
    }
    // 移除resize事件
    window.removeEventListener("resize", this.screenAdapter);
  },
  methods: {
    // 初始化echartsInstance对象
    initChart() {
      this.echartInstance = this.$echarts.init(this.$refs.rank_ref, this.theme);
      // 对图表初始化配置的控制
      const initOption = {
        title: {
          text: "▎地区销售排行",
          left: 20,
          top: 20
        },
        grid: {
          top: "40%",
          left: "5%",
          right: "5%",
          bottom: "5%",
          containLabel: true
        },
        tooltip: {
          show: true
        },
        xAxis: {
          type: "category"
        },
        yAxis: {
          type: "value"
        },
        series: [
          {
            type: "bar"
          }
        ]
      };
      this.echartInstance.setOption(initOption);
      //   事件监听
      this.echartInstance.on("mouseover", () => {
        clearInterval(this.timerId);
        this.timerId = null;
      });
      this.echartInstance.on("mouseout", () => {
        this.startInterval();
      });
    },
    // 获取服务器数据
    async getData() {
      const { data: ret } = await this.$http.get("rank");
      this.allData = ret;
      this.allData.sort((a, b) => {
        return b.value - a.value;
      });
      this.updateChart();
      //   调用定时器
      this.startInterval();
    },
    // 更新图表
    updateChart() {
      const colorArr = [
        ["#0BA82C", "#4FF778"],
        ["#2E72BF", "#23E5E5"],
        ["#5052EE", "#AB6EE5"]
      ];
      const provinceArr = this.allData.map(v => v.name);
      const valueArr = this.allData.map(v => v.value);
      const dataOption = {
        xAxis: {
          data: provinceArr
        },
        // 区域缩放
        dataZoom: {
          // 隐藏dataZoom
          show: false,
          startValue: this.startValue,
          endValue: this.endValue
        },
        series: [
          {
            data: valueArr,
            itemStyle: {
              // 颜色可以是一个函数返回值
              color: arg => {
                let targetColorArr = null;
                // console.log("arg:", arg);
                if (arg.value > 300) {
                  targetColorArr = colorArr[0];
                } else if (arg.value > 200) {
                  targetColorArr = colorArr[1];
                } else {
                  targetColorArr = colorArr[2];
                }
                return new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: targetColorArr[0]
                  },
                  {
                    offset: 1,
                    color: targetColorArr[1]
                  }
                ]);
              }
            }
          }
        ]
      };
      this.echartInstance.setOption(dataOption);
    },
    // 图表对屏幕的适配
    screenAdapter() {
      const titleFontSize = (this.$refs.rank_ref.offsetWidth / 100) * 3.6;
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize
          }
        },
        series: [
          {
            barWidth: titleFontSize,
            itemStyle: {
              barBorderRadius: [titleFontSize / 2, titleFontSize / 2, 0, 0]
            }
          }
        ]
      };
      this.echartInstance.setOption(adapterOption);
      // 手动调用图表对象的resize以适配
      this.echartInstance.resize();
    },
    // 平移动画定时器
    startInterval() {
      if (this.timerId) {
        clearInterval(this.timerId);
        this.timerId = null;
      }
      this.timerId = setInterval(() => {
        this.startValue++;
        this.endValue++;
        if (this.endValue > this.allData.length - 1) {
          this.startValue = 0;
          this.endValue = 9;
        }
        this.updateChart();
      }, 2000);
    }
  }
};
</script>

<style lang="less" scoped></style>
